/**
 * Created by Administrator on 2016/6/22.
 */
'use strict';
swan_pc
    .factory('UserOppSelect',function($resource,DateUtils){
        return $resource('api/opps',{
            'query':{
                method:'GET',
                isArray:true
            }
        });
    })
    .factory('UserContactsSelect',function($resource,DateUtils){
        return $resource('api/user_contacts3',{
            'query':{
                method:'GET',
                isArray:true
            }
        });
    })
    .factory('Friends',function($resource,DateUtils){
        return $resource('api/friendPermissionWhoGiveMeViewPermission',{
            'query':{
                method:'GET',
                isArray:true
            }
        });
    })
    .factory('ClipByLevel',function($resource,DateUtils){
        return $resource('api/clips/:level/byLevel',{level:"@level"},{
            'query':{
                method:'GET',
                isArray:true
            }
        })
    })
    .factory('Clip', function ($resource, DateUtils) {
        return $resource('api/clips/:clipid', {clipid:'@clipid'}, {
            'query': {method: 'GET', isArray: true},
            'get': {
                method: 'GET',
                transformResponse: function (data) {
                    data = angular.fromJson(data);
                    data.startTime = DateUtils.convertDateTimeFromServer(data.startTime);
                    data.endTime = DateUtils.convertDateTimeFromServer(data.endTime);
                    data.deletedAt = DateUtils.convertDateTimeFromServer(data.deletedAt);
                    return data;
                }
            },
            'update': {method: 'PUT'},
            'delete':{method:"DELETE"},
            'save':{
                method:'POST'
            }
        });
    })
    .factory('UserClip',function($resource,DateUtils){
        return $resource('/api/users/:id/clips',{id:'@id'},{
            'query':{method:'GET',isArray:true},
            'get': {
                method: 'GET',
                isArray:true,
                transformResponse: function (data) {
                    data = angular.fromJson(data);
                    data.startTime = DateUtils.convertDateTimeFromServer(data.startTime);
                    data.endTime = DateUtils.convertDateTimeFromServer(data.endTime);
                    data.deletedAt = DateUtils.convertDateTimeFromServer(data.deletedAt);
                    return data;
                }
            },
            "save":{
                method:"POST"
            }
        })
    })
    .factory('TimeExchange',function(){
        return{
            GMTtoBjTime:function(value){
                //
                if(value=="" || value==null){
                    return "";
                }
                else{
                    var tempValue = value.replace("T", " ");
                    var dateBefore = tempValue.slice(0,10);

                    var timeBefore = tempValue.slice(11,19);
                    var dateArray = dateBefore.split("-");
                    var timeArray = timeBefore.split(":");
                    //注意，Date对象中的getMonth() 返回0~11

                    var feedDate = Date.UTC(dateArray[0],dateArray[1]-1,dateArray[2],timeArray[0],timeArray[1],timeArray[2],0) + 8*60*60;
                    var now = new Date();
                    now.setTime(feedDate);

                    if (now.getMonth()<10){
                        var m=0;
                        m=now.getMonth()+1;
                        var month = "0" + m;
                    }else{
                        var month = now.getMonth()+1;
                    }

                    if (now.getDate()<10){
                        var d=0;
                        d=now.getDate()+1;
                        var date = "0" + d;
                    }else{
                        var date = now.getDate();
                    }
                    var dateAfter = now.getFullYear() + "-" + month + "-" + date;
                    //                var dateAfter = month + "月" + date + "日";

                    if (now.getHours()<10){
                        var hour = "0" + now.getHours();
                    }else{
                        var hour = now.getHours();
                    }

                    if (now.getMinutes()<10){
                        var minute = "0" + now.getMinutes();
                    }else{
                        var minute = now.getMinutes();
                    }
                    var timeAfter = hour + ":" + timeArray[1]+":"+timeArray[2];

                    var timeFinal = dateAfter + "  " + timeAfter;
                    return timeFinal;
                }
            },
            UTCtoGMT:function(d){
                function pad(n){
                    return n<10 ? '0'+n : n;
                }
                return d.getUTCFullYear()+'-'+ pad(d.getUTCMonth()+1)+'-'
                    + pad(d.getUTCDate())+'T' + pad(d.getUTCHours())+':'
                    + pad(d.getUTCMinutes())+':' + pad(d.getUTCSeconds())+'Z';
            },
            StringtoLocalTime:function(time){
                //Sun Jun 12 2016 00:00:00 GMT+0800 (ä¸­å½æ åæ¶é´)
                var timer=time.split(" ");
                var month;
                var monthEnglish=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
                for(var i=0;i<monthEnglish.length;i++){
                    if(timer[1]==monthEnglish[i]){
                        month=i+1;
                    }
                }
                var day=timer[2];
                var year=timer[3];
                var timeA=timer[4];
                var newTime=year+"-"+month+"-"+day+" "+timeA;
                return newTime;
            }
        }
    })
    .factory('TimeCheck',function(){
        return{
            timecheck:function(start,end){
                start=start.toLocaleString();
                end=end.toLocaleString();
                //console.log("toLocalString:"+start);
                //console.log("toLocalString:"+end);
                start=start.split(" ");
                end=end.split(" ");
                var flag=0;
                if(start[0]!=end[0]){
                    //表示跨天
                    flag=0;
                    return flag;
                }
                var startTime=start[1].substring(2,10);
                var endTime=end[1].substring(2,10);
                var startFlag=start[1].substring(0,2);
                var endFlag=end[1].substring(0,2);
                //console.log("startFlag:"+startFlag);
                //console.log("endFlag:"+endFlag);
                //console.log("startTime:"+startTime);
                //console.log("endTime:"+endTime);
                var startFlag12=0;
                var endFlag12=0;
                if(startTime[0]=="1"&&startTime[1]=="2"){
                    startFlag12=1;
                }
                if(endTime[0]=="1"&&endTime[1]=="2"){
                    endFlag12=1;
                }
                if(startFlag=="上午") {
                    if (startTime[2] == ":" && startFlag12==1) {
                        var startMin = ((startTime[0] - '0') * 10 + (startTime[1] - '0')-12) * 60 + (startTime[3] - '0') * 10 + (startTime[4] - '0');
                    }
                    if(startTime[2] ==":" && startFlag12==0){
                        var startMin = ((startTime[0] - '0') * 10 + (startTime[1] - '0')) * 60 + (startTime[3] - '0') * 10 + (startTime[4] - '0');
                    }
                    if (startTime[1] == ":") {
                        var startMin = (startTime[0] - '0') * 60 + (startTime[2] - '0') * 10 + (startTime[3] - '0');
                    }
                }
                if(startFlag=="下午"){
                    if(startTime[2] == ":"&&startFlag12==1){
                        var startMin = ((startTime[0] - '0') * 10 + (startTime[1] - '0')) * 60 + (startTime[3] - '0') * 10 + (startTime[4] - '0');
                    }
                    if (startTime[2] == ":" &&startFlag==0) {
                        var startMin = ((startTime[0] - '0') * 10 + (startTime[1] - '0')+12) * 60 + (startTime[3] - '0') * 10 + (startTime[4] - '0');
                    }
                    if (startTime[1] == ":") {
                        var startMin = (startTime[0] - '0'+12) * 60 + (startTime[2] - '0') * 10 + (startTime[3] - '0');
                    }
                }
                if(endFlag=="上午") {
                    if(endTime[2]==":"&&endFlag12==1){
                        var endMin = ((endTime[0] - '0') * 10 + (endTime[1] - '0')-12) * 60 + (endTime[3] - '0') * 10 + (endTime[4] - '0');
                    }
                    if (endTime[2] == ":"&&endFlag12==0) {
                        var endMin = ((endTime[0] - '0') * 10 + (endTime[1] - '0')) * 60 + (endTime[3] - '0') * 10 + (endTime[4] - '0');
                    }
                    if (endTime[1] == ":") {
                        var endMin = (endTime[0] - '0') * 60 + (endTime[2] - '0') * 10 + (endTime[3] - '0');
                    }
                }
                if(endFlag=="下午"){
                    if(endTime[2]==":"&&endFlag12==1){
                        var endMin = ((endTime[0] - '0') * 10 + (endTime[1] - '0')) * 60 + (endTime[3] - '0') * 10 + (endTime[4] - '0');
                    }
                    if (endTime[2] == ":"&&endFlag12==0) {
                        var endMin = ((endTime[0] - '0') * 10 + (endTime[1] - '0')+12) * 60 + (endTime[3] - '0') * 10 + (endTime[4] - '0');
                    }
                    if (endTime[1] == ":") {
                        var endMin = (endTime[0] - '0'+12) * 60 + (endTime[2] - '0') * 10 + (endTime[3] - '0');
                    }
                }
                if(endMin<startMin){
                    flag=3;
                    return flag;
                }
                if((endMin-startMin)<45){
                    flag=1;
                    return flag;
                }
                flag=2;
                return flag;
            }
        }
    })
